package com.test.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.dto.UserDTO;
import com.test.entity.Result;
import com.test.entity.ResultList;
import com.test.entity.User;
import com.test.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  controller
 *
 * @author kuke
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
/*
    @ApiOperation(value = "新增")
    @ApiImplicitParam(name = "mo", value = "新增", required = true, dataType = "KoOrderDTO")
    @GetMapping("/getList")
    public List<User> getList() {
        List<User> list = userService.list();
        System.out.println(JSON.toJSONString(list));
        userService.queryUserList(1L);

        return list;
    }
*/

    @ApiOperation(value = "新增")
    @ApiImplicitParam(name = "mo", value = "新增", required = true, dataType = "User")
    @PostMapping("/save")
    public Result save(@RequestBody User mo){
       userService.insert(mo);     
    return Result.success("保存成功");
    }

    @ApiOperation(value = "根据id删除")
    @ApiImplicitParam(name = "id", value = "删除", required = true, dataType = "Long")
    @PostMapping("/deleteById")
    public Result delete(@RequestParam("id") Long id){
        userService.deleteById(id);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "条件查询")
    @ApiImplicitParam(name = "mo", value = "条件查询", required = true, dataType = "User")
    @PostMapping("/getList")
    public Result list(@RequestBody User mo){
        List<User> users = userService.queryList(mo);
        return Result.success(users);
    }

    @ApiOperation(value = "列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(name = "mo", value = "条件查询", required = true, dataType = "KoOrderDelivery")

    })
    @GetMapping("/getPagelist")
    public Object list(@RequestBody UserDTO mo){
        IPage<User> page = userService.getPageList(mo);
        return ResultList.instance(page.getRecords(),page.getTotal());
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(paramType = "path", name = "id", value = "详情", required = true, dataType = "String")
    @GetMapping("/getById")
    public Result get(@RequestParam("id") Long id){
        User user = userService.queryById(id);
        return Result.success("","查询成功",user);
    }

    @ApiOperation(value = "根据id修改")
    @ApiImplicitParam()
    @PostMapping("/updateById")
    public Result update(@RequestBody User mo){
        userService.update(mo);
        return Result.success("更新成功");
    }

}




